﻿using famails.common;
using famails.dto;
using famails.bus;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Script.Services;
using System.Text.RegularExpressions;
using System.Configuration;

namespace famails.BackEnd.page
{
    public partial class create_event : System.Web.UI.Page
    {
        MailConfigBUS mailConfigBus = null;
        MailGroupBUS mailGroupBus = null;
        EventBUS eventBus = null;
        SignatureBUS signBus = null;
        UserLoginDTO userLogin = null;
        DataTable SignIn = null;
        log4net.ILog logs = log4net.LogManager.GetLogger("ErrorRollingLogFileAppender");
        log4net.ILog logs_info = log4net.LogManager.GetLogger("InfoRollingLogFileAppender");
        protected void Page_Load(object sender, EventArgs e)
        {
            userLogin = getUserLogin();
            if (!IsPostBack)
            {
                try
                {
                    InitialBUS();
                    LoadMailGroupLists();
                    LoadMailConfigList();
                    // Khoi tao session for store contentSendEvent
                    ContentSendEventBUS cseBus = new ContentSendEventBUS();
                    Session["listContentSendEvent"] = cseBus.GetById(0);

                    hdfEventId.Value = Request.QueryString["EventId"] + "";
                    LoadEventList(); LoadSignatureList();
                    LoadContentList();
                    drlMailGroup.Enabled = Request.QueryString["EventId"] + "" == "";
                }
                catch (Exception ex)
                {
                    logs.Error(userLogin.Username + "-Create-Event - Page_Load", ex);
                    pnError.Visible = true;
                    lblError.Text = ex.Message;
                }

            }
        }

        private void LoadContentList()
        {
            try
            {
                SendContentBUS scBus = new SendContentBUS();
                DataTable dtContent = scBus.GetAllSendContent(getUserLogin().UserId, Common.contentType.AutoResponder);
                DataRow row = dtContent.NewRow();
                row["Subject"] = "Chọn nội dung";
                row["Id"] = "-1";
                dtContent.Rows.InsertAt(row, 0);
                if (dtContent.Rows.Count > 0)
                {

                    drlContent.DataSource = dtContent;
                    drlContent.DataTextField = "Subject";
                    drlContent.DataValueField = "Id";
                    drlContent.DataBind();
                }
            }
            catch (Exception ex)
            {
                logs.Error(userLogin.Username + "-Create-Event - LoadContentList", ex);
            }
        }

        private void createTableSign()
        {
            SignIn = new DataTable("SignIn");
            DataColumn Id = new DataColumn("Id");
            Id.DataType = System.Type.GetType("System.Int32");
            DataColumn SignatureName = new DataColumn("SignatureName");
            DataColumn[] key = { Id };
            SignIn.Columns.Add(Id);
            SignIn.Columns.Add(SignatureName);
            SignIn.PrimaryKey = key;
        }
        private void LoadSignatureList()
        {
            try
            {

                signBus = new SignatureBUS();
                UserLoginDTO userLogin = getUserLogin();
                DataTable tblSignList = signBus.GetByUserId(userLogin.UserId);
                createTableSign();
                if (tblSignList.Rows.Count > 0)
                {
                    DataRow rowE = SignIn.NewRow();
                    rowE["Id"] = 0;
                    rowE["SignatureName"] = "Chọn chữ ký";
                    SignIn.Rows.Add(rowE);
                    foreach (DataRow RowItem in tblSignList.Rows)
                    {
                        rowE = SignIn.NewRow();
                        rowE["Id"] = RowItem["id"];
                        rowE["SignatureName"] = RowItem["SignatureName"];
                        SignIn.Rows.Add(rowE);
                    }
                    drlSign.DataSource = SignIn;
                    drlSign.DataTextField = "SignatureName";
                    drlSign.DataValueField = "id";
                    drlSign.DataBind();
                }
            }
            catch (Exception)
            {
            }
        }
        private void LoadContentSendEventList(DataTable dataTable)
        {
            try
            {
                SendContentBUS scBus = new SendContentBUS();
                if (dataTable.Rows.Count > -1)
                {
                    dataTable.DefaultView.Sort = "orderno";
                    dataTable = dataTable.DefaultView.ToTable();
                    dlContentSendEvent.DataSource = dataTable;
                    dlContentSendEvent.DataBind();
                    for (int i = 0; i < dataTable.Rows.Count; i++)
                    {
                        //Label lblID = (Label)dlContentSendEvent.Items[i].FindControl("lblID");
                        //lblID.Text = dataTable.Rows[i]["id"] + "";
                        Label lblNo = (Label)dlContentSendEvent.Items[i].FindControl("lblNo");
                        lblNo.Text = dataTable.Rows[i]["orderno"].ToString(); ; (i + 1).ToString();

                        Label lbtContent = (Label)dlContentSendEvent.Items[i].FindControl("lbtContent");
                        //String contentId = dataTable.Rows[i]["ContentId"].ToString();
                        //DataTable dtContent = scBus.GetByID(int.Parse(contentId));
                        //if (dtContent.Rows.Count > 0)
                        //{
                        //    lbtContent.Text = dtContent.Rows[0]["Subject"].ToString();
                        //}
                        lbtContent.Text = dataTable.Rows[i]["Subject"].ToString();
                        Label lblHour = (Label)dlContentSendEvent.Items[i].FindControl("lblHour");
                        lblHour.Text = dataTable.Rows[i]["HourSend"].ToString() + " Giờ sau";

                        string id = dataTable.Rows[i]["Id"].ToString();
                        LinkButton lbtDelete = (LinkButton)dlContentSendEvent.Items[i].FindControl("lbtContentDelete");
                        lbtDelete.CommandArgument = id;

                    }
                }
            }
            catch (Exception ex)
            {
                logs.Error(userLogin.Username + "-Create-Event - LoadContentSendEventList", ex);
            }

        }

        private UserLoginDTO getUserLogin()
        {
            if (Session["us-login"] != null)
            {
                return (UserLoginDTO)Session["us-login"];
            }
            else Response.Redirect("~");
            return null;
        }
        protected void lbtInsertSignature_Click(object sender, EventArgs e)
        {
            try
            {
                int signId = int.Parse(((LinkButton)sender).CommandArgument.ToString());
                signBus = new SignatureBUS();
                DataTable tblSign = signBus.GetById(signId);
                if (tblSign.Rows.Count > 0)
                {
                    txtBody.Text = txtBody.Text + "\n" + tblSign.Rows[0]["SignatureContent"].ToString();
                }
            }
            catch (Exception ex)
            {

                logs.Error(userLogin.Username + "-Create-Event - lbtInsertSignature_Click", ex);
            }

        }

        private void LoadEventList()
        {
            try
            {
                string EventId = this.hdfEventId.Value;
                int ID = 0;
                if (EventId != "")
                {
                    ID = int.Parse(EventId);
                    DataTable dtEvent = eventBus.GetByID(ID);
                    if (dtEvent.Rows.Count > 0)
                    {

                        txtAutoName.Text = dtEvent.Rows[0]["Subject"].ToString();
                        drlMailConfig.SelectedValue = dtEvent.Rows[0]["ConfigId"].ToString();
                        drlMailGroup.SelectedValue = dtEvent.Rows[0]["GroupId"].ToString();
                        txtBody.Text = dtEvent.Rows[0]["Body"].ToString();
                        if (dtEvent.Rows[0]["ResponeUrl"].Equals("Default"))
                        {
                            chkDefaultPage.Checked = true;
                            txtResponeUrl.Enabled = false;
                        }
                        else
                        {

                            chkDefaultPage.Checked = false;
                            txtResponeUrl.Text = dtEvent.Rows[0]["ResponeUrl"].ToString();
                        }


                        // Hien thi ConentSendEvent list.
                        ContentSendEventBUS cseBus = new ContentSendEventBUS();
                        DataTable tblContent = cseBus.GetByEventId(ID);
                        if (tblContent.Rows.Count > 0)
                        {
                            Session["listContentSendEvent"] = tblContent;
                            LoadContentSendEventList(tblContent);
                        }

                    }
                }
            }
            catch (Exception ex)
            {

                logs.Error(userLogin.Username + "-Create-Event - LoadEventList", ex);
            }

        }

        private void LoadMailConfigList()
        {
            try
            {
                DataTable dtMailConfig = new DataTable();
                UserLoginDTO userLogin = getUserLogin();
                if (userLogin.DepartmentId == 1)
                {
                    dtMailConfig = mailConfigBus.GetAll();
                }
                else
                {
                    dtMailConfig = mailConfigBus.GetByUserId(userLogin.UserId);
                }
                drlMailConfig.Items.Clear();
                drlMailConfig.DataSource = dtMailConfig.DefaultView;
                drlMailConfig.DataTextField = "Email";
                drlMailConfig.DataValueField = "Id";
                drlMailConfig.DataBind();
            }
            catch (Exception ex)
            {

                logs.Error(userLogin.Username + "-Create-Event - LoadMailConfigList", ex);
            }
        }



        private void LoadMailGroupLists()
        {
            try
            {
                DataTable dtMailGroup = null;

                if (getUserLogin().DepartmentId == 1)
                {
                    dtMailGroup = mailGroupBus.GetAllNew();
                }
                else //if (getUserLogin().DepartmentId == 2)
                {
                    dtMailGroup = mailGroupBus.GetMailGroupByUserId(getUserLogin().UserId);
                }
                //if (getUserLogin().DepartmentId == 3)
                //{
                //    dtMailGroup = mailGroupBus.GetAllNewDepart3(getUserLogin().UserId);
                //}

                if (dtMailGroup.Rows.Count > 0)
                {
                    drlMailGroup.Items.Clear();
                    drlMailGroup.DataSource = dtMailGroup.DefaultView;
                    drlMailGroup.DataTextField = "Name";
                    drlMailGroup.DataValueField = "Id";
                    drlMailGroup.DataBind();

                }
                else
                {
                    //pnSelectGroup.Visible = false;
                }
            }
            catch (Exception ex)
            {

                logs.Error(userLogin.Username + "-Create-Event - LoadMailGroupLists", ex);
            }
        }

        private void InitialBUS()
        {
            mailConfigBus = new MailConfigBUS();
            mailGroupBus = new MailGroupBUS();
            eventBus = new EventBUS();
        }
        protected void btnSaveContentAndEvent_Click(object sender, EventArgs e)
        {
            try
            {

                pnError.Visible = false;
                InitialBUS();
                String eventId = hdfEventId.Value.ToString();
                EventDTO eventDto = getEvent();
                if (checkData() != "")
                {
                    pnSuccess.Visible = false;
                    pnError.Visible = true;
                    lblError.Text = checkData();
                    return;
                }

                int status = 1;// insert
                if (eventId == "" || eventId == null)//insert new
                {
                    int eId = eventBus.tblEvent_insert(eventDto);
                    eventDto.EventId = eId;
                    hdfEventId.Value = eId.ToString();

                    DataTable dtContent = (DataTable)Session["listContentSendEvent"];
                    eventBus.tblEventCustomer_Insert(eventDto);
                    logs_info.Info(userLogin.Username + " create event(" + eId + ") with " + dtContent.Rows.Count + " send times");

                }
                else //update
                {
                    status = 2;
                    eventDto.EventId = int.Parse(eventId);
                    eventBus.tblEvent_Update(eventDto);

                }
                // Store for ContentSendEvent table.
                saveContentSendEvent(eventDto.EventId);
                SendRegisterBUS bus = new SendRegisterBUS();
                bus.addSendMailFromEvent(eventDto.EventId, userLogin.UserId);
                LoadEventList();
                pnSuccess.Visible = true;

                if (status == 1)
                {
                    lblSuccess.Text = "Bạn vừa thêm thành công sự kiện !";
                }
                else
                {
                    lblSuccess.Text = "Thông tin của sự kiện đã được cập nhật !";
                }
                //txtSubject.Text = txtBody.Text = txtAutoName.Text = "";
                //Session["listContentSendEvent"] = null;
                pnError.Visible = false;
                // LoadEventList();
            }
            catch (Exception ex)
            {
                logs.Error(userLogin.Username + "Create-Event - btnSaveEvent_Click", ex);
                pnError.Visible = true;
                lblError.Text = ex.Message;
            }

        }
        private string checkData()
        {
            string msg = "";
            if (txtAutoName.Text == "")
            {
                msg = "Vui lòng nhập vào tiêu đề cho sự kiện !";
            }
            DataTable dtContent = (DataTable)Session["listContentSendEvent"];
            if (dtContent == null || dtContent.Rows.Count == 0)
                msg = "Vui lòng cấu hình danh sách gửi từng phần của sự kiện này !";
            //else if (txtBody.Text == "")
            //{
            //    msg = "Vui lòng nhập vào nội dung của sự kiện này !";
            //}
            /*else
            {

                
                else
                {
                    if (Request.QueryString["EventId"] + "" == "")
                    {
                        SendRegisterBUS bus = new SendRegisterBUS();
                        int i = 0;
                        int.TryParse(ConfigurationManager.AppSettings["MinHourForGroupSend"], out i);
                        if (i == 0) i = 1;
                        int count = bus.GetRecentGroupSend(drlMailGroup.SelectedValue, userLogin.UserId, i);
                        if (count > 0)
                            msg = "Mỗi đợt gửi cùng nhóm mail phải cách nhau ít nhất " + i + " giờ";
                    }
                }
            }*/
            return msg;
        }
        protected EventDTO getEvent()
        {

            string subject = txtAutoName.Text;
            string subscribe = "";
            string body = txtBody.Text;
            int configId = int.Parse(drlMailConfig.SelectedValue.ToString());
            string responeUrl = "";
            if (chkDefaultPage.Checked == true)
            {
                responeUrl = "Default";
            }
            else
            {
                responeUrl = txtResponeUrl.Text;

                if (responeUrl == null || responeUrl == "")
                {
                    responeUrl = txtResponeUrl.Text;
                }
            }

            int groupId = int.Parse(drlMailGroup.SelectedValue);
            // Set some default value.
            string confirmContent = "";
            string confirmFlag = "t";

            // Initial eventDto object.
            EventDTO eventDto = new EventDTO(subject, "", subscribe, body, configId,
                DateTime.Now, DateTime.Now, responeUrl, confirmContent, confirmFlag, getUserLogin().UserId, groupId);

            return eventDto;
        }
        protected void btnCreateNew_Click(object sender, EventArgs e)
        {
            try
            {
                resetForm();
                hdfEventId.Value = "";
                Response.Redirect(Request.RawUrl);

                // Set null value for listContentSendEvent session.
                ContentSendEventBUS cseBus = new ContentSendEventBUS();
                Session["listContentSendEvent"] = cseBus.GetById(0);

            }
            catch (Exception ex)
            {
                logs.Error(userLogin.Username + "Create-Event - btnCreateNew_Click", ex);
                pnError.Visible = true;
                lblError.Text = ex.Message;
            }

        }
        protected void resetForm()
        {
            txtSubject.Text = "";
            txtBody.Text = "";
            drlMailConfig.SelectedIndex = 0;
            drlMailGroup.SelectedIndex = 0;
        }
        protected DateTime convertStringToDate(string strDate)
        {
            int month = int.Parse(strDate.Substring(0, 2));
            int day = int.Parse(strDate.Substring(3, 2));
            int year = int.Parse(strDate.Substring(6, 4));
            int hours = int.Parse(strDate.Substring(11, 2));
            int minute = int.Parse(strDate.Substring(14, 2));
            DateTime dDate = new DateTime(year, month, day, hours, minute, 00);
            return dDate;
        }
        protected String convertDateToString(DateTime date)
        {
            int month = date.Month;
            int day = date.Day;
            int year = date.Year;
            int hours = date.Hour;
            int minute = date.Minute;
            String strDate = (month < 10) ? "0" + month : month + "";
            strDate += "/";
            strDate += (day < 10) ? "0" + day : day + "";
            strDate += "/";
            strDate += year + " ";
            strDate += (hours < 10) ? "0" + hours : hours + "";
            strDate += ":";
            strDate += (minute < 10) ? "0" + minute : minute + "";
            return strDate;

        }


        protected void lbtEdit_Click(object sender, EventArgs e)
        {
            try
            {
                InitialBUS();
                visibleMessage(false);
                int eventId = int.Parse(((LinkButton)sender).CommandArgument.ToString());
                DataTable dtEvent = eventBus.GetByID(eventId);
                if (dtEvent.Rows.Count > 0)
                {
                    DataRow row = dtEvent.Rows[0];
                    hdfEventId.Value = row["EventId"].ToString();
                    txtSubject.Text = row["Subject"].ToString();
                    drlMailConfig.SelectedValue = row["ConfigId"].ToString();
                    drlMailGroup.SelectedValue = row["GroupId"].ToString();
                    txtBody.Text = row["Body"].ToString();
                    if (row["ResponeUrl"].Equals("Default"))
                    {
                        chkDefaultPage.Checked = true;
                        txtResponeUrl.Enabled = false;
                    }
                    else
                    {
                        txtResponeUrl.Text = row["ResponeUrl"].ToString();
                    }
                }

                // Hien thi ConentSendEvent list.
                ContentSendEventBUS cseBus = new ContentSendEventBUS();
                DataTable tblContent = cseBus.GetByEventId(eventId);
                if (tblContent.Rows.Count > 0)
                {
                    Session["listContentSendEvent"] = tblContent;
                    LoadContentSendEventList(tblContent);
                }

            }
            catch (Exception ex)
            {
                logs.Error(userLogin.Username + "Create-Event - lbtEdit_Click", ex);
                pnError.Visible = true;
                lblError.Text = ex.Message;
            }

        }
        protected void visibleMessage(bool vis)
        {
            pnError.Visible = vis;
            pnSuccess.Visible = vis;
        }
        

        protected void btnGenerate_Click(object sender, EventArgs e)
        {
            try
            {
                if (checkData() != "")
                {
                    pnSuccess.Visible = false;
                    pnError.Visible = true;
                    lblError.Text = checkData();
                    return;
                }
                String eventId = hdfEventId.Value.ToString();
                EventDTO eventDto = getEvent();
                InitialBUS();
                if (eventId == "" || eventId == null)//them moi
                {
                    int rs = eventBus.tblEvent_insert(eventDto);
                    eventId = rs.ToString();
                }
                else //cap nhat
                {
                    eventDto.EventId = int.Parse(eventId);
                    eventBus.tblEvent_Update(eventDto);
                }
                Session["eventId"] = eventId;
                Session["GroupId"] = drlMailGroup.SelectedValue;
                Response.Redirect("generate.aspx?eventid=" + eventId + "&groupid=" + drlMailGroup.SelectedValue, false);
            }
            catch (Exception ex)
            {
                logs.Error(userLogin.Username + "Create-Event - btnGenerate_Click", ex);
                pnError.Visible = true;
                lblError.Text = ex.Message;
            }

        }
        protected void chkDefaultPage_CheckedChanged(object sender, EventArgs e)
        {
            if (chkDefaultPage.Checked == true)
            {
                txtResponeUrl.Enabled = false;
            }
            else
            {
                txtResponeUrl.Enabled = true;
            }
        }

        protected void lbtEditSignature_Click(object sender, EventArgs e)
        {
            Response.Redirect("create-signature.aspx");
        }
        protected void btnSaveContent_Click(object sender, EventArgs e)
        {

            pnError.Visible = false;
            SendContentBUS scBus = new SendContentBUS();
            if (txtSubject.Text.Trim() == "") { PanelHourError.Visible = true; lblHourError.Text = "Vui lòng nhập tiêu đề"; return; }
            if (txtBody.Text.Trim() == "") { PanelHourError.Visible = true; lblHourError.Text = "Vui lòng nhập nội dung"; return; }
            int temp = 0;
            if (int.TryParse(txtHour.Text, out temp) && temp < 0) { PanelHourError.Visible = true; lblHourError.Text = "Nhập sai giá trị giờ"; return; }
            if (int.TryParse(txtOrderNo.Text, out temp) && temp < 0) { PanelHourError.Visible = true; lblHourError.Text = "Nhập sai thứ tự gửi"; return; }
            try
            {
                PanelHourError.Visible = false;

                int hour = int.Parse(txtHour.Text);
                int orderno = int.Parse(txtOrderNo.Text);
                DataTable dtContent = (DataTable)Session["listContentSendEvent"];

                if (hdfContentIdEdit.Value + "" == "" && hour == 0 && dtContent != null && dtContent.Select("hoursend=" + hour).Length != 0) { PanelHourError.Visible = true; lblHourError.Text = "Trùng giá trị giờ!"; return; }

                int contentId = int.Parse(drlContent.SelectedValue);
                string contentSubject = drlContent.SelectedItem.ToString();
                string subject = this.txtSubject.Text;
                string body = this.txtBody.Text;
                if (hdfContentIdEdit.Value != "")
                {
                    DataRow[] rows = dtContent.Select("id=" + hdfContentIdEdit.Value);
                    if (rows.Length > 0)
                    {
                        rows[0]["subject"] = txtSubject.Text;
                        rows[0]["body"] = txtBody.Text;
                        rows[0]["hoursend"] = txtHour.Text;
                        rows[0]["orderno"] = txtOrderNo.Text;
                        if (txtHour.Text.Trim() == "0")
                            rows[0]["orderno"] = 0;
                        if (drlContent.SelectedValue + "" != "")
                            rows[0]["contentid"] = drlContent.SelectedValue;
                        hdfContentIdEdit.Value = "";
                        adhjskd.InnerText = "Thêm";
                        //txtHour.Enabled = true;

                        txtHour.Text = txtSubject.Text = txtBody.Text = "";

                    }
                }
                else
                {
                    Random rnd = new Random();
                    int id = rnd.Next(5000);
                    DataRow row = dtContent.NewRow();
                    row["id"] = id * -1;
                    row["ContentId"] = contentId;
                    row["HourSend"] = hour;
                    row["OrderNo"] = orderno;
                    if (txtHour.Text.Trim() == "0")
                        row["orderno"] = 0;
                    row["subject"] = subject;
                    row["body"] = body;
                    dtContent.Rows.Add(row);
                }

                PanelHourError.Visible = false;
                pnSuccess.Visible = true;
                lblSuccess.Text = "Để hoàn thành cập nhật, bạn bấm 'Lưu sự kiện'.";

                LoadContentSendEventList(dtContent);
                //this.txtBody.Text = "";
                //this.txtSubject.Text = "";
                drlContent.SelectedIndex = 0;

            }
            catch (Exception ex)
            {
                logs.Error(userLogin.Username + "Create-Event - btnSaveContent_Click", ex);
                PanelHourError.Visible = true;
                lblHourError.Text = "Nhập sai giá trị giờ !";
            }
        }
        protected void lbtContentDelete_Click(object sender, EventArgs e)
        {
            try
            {
                ContentSendEventBUS cseBus = new ContentSendEventBUS();
                int contentId = int.Parse(((LinkButton)sender).CommandArgument.ToString());
                DataTable dtContent = (DataTable)Session["listContentSendEvent"];
                if (dtContent.Rows.Count > 0)
                {
                    for (int i = 0; i < dtContent.Rows.Count; i++)
                    {
                        DataRow row = dtContent.Rows[i];
                        if (row["Id"].Equals(contentId))
                        {
                            // Xóa trong db, nếu có tồn tại.
                            if (hdfEventId.Value != null)
                            {
                                //if (cseBus.CheckContentSent(contentId))
                                //{
                                cseBus.tblContentSendEvent_Delete(contentId); dtContent.Rows.Remove(row);
                                //}
                                //else
                                //{
                                //    PanelHourError.Visible = true;
                                //    lblHourError.Text = "Nội dung này đã được gửi thư, bạn không thể xóa!";
                                //}
                            }
                            else
                                dtContent.Rows.Remove(row);

                        }
                    }
                }
                LoadContentSendEventList(dtContent);
            }
            catch (Exception ex)
            {
                logs.Error(userLogin.Username + "Create-Event - btnDeleteContent_Click", ex);
            }
        }

        protected void saveContentSendEvent(int eventId)
        {
            try
            {
                ContentSendEventBUS cseBus = new ContentSendEventBUS();
                DataTable dtContent = (DataTable)Session["listContentSendEvent"];
                if (dtContent.Rows.Count > 0)
                {
                    for (int i = 0; i < dtContent.Rows.Count; i++)
                    {
                        DataRow row = dtContent.Rows[i];
                        ContentSendEventDTO cseDto = new ContentSendEventDTO();
                        cseDto.Id = int.Parse(row["Id"].ToString());
                        cseDto.EventId = eventId;
                        cseDto.ContentId = int.Parse(row["ContentId"].ToString());
                        cseDto.HourSend = int.Parse(row["HourSend"].ToString());
                        cseDto.OrderNo = int.Parse(row["OrderNo"].ToString());
                        cseDto.Subject = row["Subject"] + "";
                        cseDto.Body = row["Body"] + "";

                        int rsUpdate = cseBus.tblContentSendEvent_Update(cseDto);
                        if (rsUpdate <= 0)
                        {
                            cseDto.Id = int.Parse(cseBus.tblContentSendEvent_insert(cseDto)+"");

                            //SendContentDTO scDTO = new SendContentDTO();
                            //scDTO = getContentDTO();
                            //int contentID = new SendContentBUS().tblSendContent_insert(scDTO);
                        }
                    }
                    Session["listContentSendEvent"] = dtContent;
                }
            }
            catch (Exception ex)
            {
                logs.Error(userLogin.Username + "Create-Event - btnSaveContent_Click", ex);
            }
        }

        private SendContentDTO getContentDTO()
        {
            SendContentDTO scDTO = new SendContentDTO();
            scDTO.Body = txtBody.Text;
            scDTO.Subject = txtSubject.Text;
            scDTO.CreateDate = DateTime.Now;
            scDTO.userId = getUserLogin().UserId;

            return scDTO;
        }
        protected void lbtContentRefresh_Click(object sender, EventArgs e)
        {
            LoadContentList();
        }
        [System.Web.Services.WebMethod]
        public static string Spam(string content)
        {
            try
            {
                string[] _content = Regex.Split(content, "//Hung-Hai//");
                SpamRuleBUS spamBUS = new SpamRuleBUS();
                DataTable spam = spamBUS.GetAll();
                float totalScoreTitle = 0;
                float totalScoreContent = 0;
                string ruleContent = "";
                float ItemScore = 0;
                string key = "";
                string titleData = _content[0];
                string contentData = _content[1];
                #region MyRegion
                #region Fisrt
                ruleContent = "<div class='toolTipBox' style='padding: 5px; margin-top: 10px; background-color: rgb(224, 236, 255);" +
                        "color: rgb(51, 51, 51); text-decoration: none; margin-bottom: 15px; font-family: Tahoma, Arial;" +
                        "font-size: 11px; font-style: normal; font-variant: normal; font-weight: normal;" +
                        "letter-spacing: normal; line-height: normal; orphans: auto; text-align: start;" +
                        "text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px;" +
                        "-webkit-text-stroke-width: 0px;'>" +
                        "<img align='left' height='16' src='../resource/images/infoballon.gif' width='20' />Thống" +
                        "kê nội dung các quy tắc vi phạm của &#39;<b>Tiêu đề gửi</b>&#39; chi tiết như bên" +
                        " dưới:</div>" +
                        "<div class='Heading2' style='font-weight: bold; color: rgb(0, 0, 0); height: 16pt;" +
                        "background-color: rgb(237, 236, 236); padding: 4px 4px 4px 10px; background-image: url(../resource/images/table_bg.gif);" +
                        "font-family: Tahoma, Arial; font-size: 11px; font-style: normal; font-variant: normal;" +
                        "letter-spacing: normal; line-height: normal; orphans: auto; text-align: start;" +
                        "text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px;" +
                        "-webkit-text-stroke-width: 0px;'>" +
                        "<div class='spamRuleBroken_row_rulename' style='float: left; padding: 3px 0px 3px 5px;'>" +
                        "Quy tắc vi phạm</div>" +
                        "<div class='spamRuleBroken_row_rulescore' style='float: right; width: 80px; text-align: right;" +
                        "padding: 3px 15px 3px 5px;'>" +
                        "Điểm</div>" +
                        "&nbsp;</div>" +
                        "<div class='spam_info spamRuleBroken_row' style='padding: 4px 8px; background-color: rgb(249, 249, 249);" +
                       " display: block; clear: both; color: rgb(103, 103, 103); font-family: Tahoma, Arial;" +
                        "font-size: 11px; font-style: normal; font-variant: normal; font-weight: normal;" +
                        "letter-spacing: normal; line-height: normal; orphans: auto; text-align: start;" +
                        "text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px;" +
                        "-webkit-text-stroke-width: 0px;'>";
                #endregion Fisrt
                foreach (DataRow RowItem in spam.Rows)
                {
                    key = " " + RowItem["Keyword"].ToString() + " ";
                    ItemScore = float.Parse(RowItem["Score"].ToString());
                    if (titleData.Contains(key) == true)
                    {
                        //Bắt đầu vòng lặp tính điểm tiêu đề
                        ruleContent += "<div class='spam_info spamRuleBroken_row' style='padding: 4px 8px; background-color: rgb(249, 249, 249);" +
                              "display: block; clear: both; color: rgb(103, 103, 103); font-family: Tahoma, Arial;" +
                              "font-size: 11px; font-style: normal; font-variant: normal; font-weight: normal;" +
                              "letter-spacing: normal; line-height: normal; orphans: auto; text-align: start;" +
                              "text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px;" +
                              "-webkit-text-stroke-width: 0px;'>";
                        ruleContent += "<div class='spamRuleBroken_row_rulename' style='float: left; padding: 3px 0px 3px 5px;'>" + "Chứa từ vi phạm '" + key + "'" + "</div>" +
                         "<div class='spamRuleBroken_row_rulescore' style='float: right; width: 80px; text-align: right;" + "padding: 3px 15px 3px 5px;'>" + ItemScore + "</div> </br>";
                        ruleContent += "</div>";
                        totalScoreTitle = totalScoreTitle + ItemScore;
                        // Kết thúc vòng lặp tính điểm tiêu đề
                    }
                }

                #region Medium
                ruleContent += "" +
                  "<div class='spam_info spamRuleBroken_row' style='padding: 4px 8px; background-color: rgb(249, 249, 249);" +
                   "display: block; clear: both; color: rgb(103, 103, 103); font-family: Tahoma, Arial;" +
                   "font-size: 11px; font-style: normal; font-variant: normal; font-weight: normal;" +
                   "letter-spacing: normal; line-height: normal; orphans: auto; text-align: start;" +
                   "text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px;" +
                   "-webkit-text-stroke-width: 0px;'>" +
                   "<div class='spamRuleBroken_graph' style='border: 1px solid gray; height: 5px; background-color: rgb(238, 238, 238);'>" +
                   "<div class='spam_notspam' style='background-color: rgb(0, 255, 0); height: 5px; width: 165.296875px;'>" +
                   "<img height='5' src='../resource/images/1x1.gif' width='1' /></div>" +
                   "</div>" +
                   "<div style='line-height: 1; margin-bottom: 5px;'>" +
                   "<br />" +
                   "Nội dung được đánh giá cao nhất<span class ='Apple-converted-space'>&nbsp;</span><span" +
                   " style='font-size: 12px; font-weight: bold;'><b>" + totalScoreTitle.ToString() + "</b></span><span class='Apple-converted-space'>&nbsp;</span>trong" +
                   " ngưỡng cho phép là 5. Điều này có nghĩa email của bạn sẽ được gửi đến đích, nhưng" +
                   "điều này không được bảo đảm tuyệt đối nó chỉ có giá trị tham khảo.</div>" +
                "</div>" +
                "<div class='toolTipBox' style='padding: 5px; margin-top: 10px; background-color: rgb(224, 236, 255);" +
                "color: rgb(51, 51, 51); text-decoration: none; margin-bottom: 15px; font-family: Tahoma, Arial;" +
                "font-size: 11px; font-style: normal; font-variant: normal; font-weight: normal;" +
                "letter-spacing: normal; line-height: normal; orphans: auto; text-align: start;" +
                "text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px;" +
                "-webkit-text-stroke-width: 0px;'>" +
                "<img align='left' height='16' src='../resource/images/infoballon.gif' width='20' />Thống" +
                " kê nội dung các quy tắc vi phạm của &#39;<b>Nội dung gửi</b>&#39; chi tiết như" +
                " bên dưới:" + "</div>" +
                 "<div class='Heading2' style='font-weight: bold; color: rgb(0, 0, 0); height: 16pt;" +
                 "background-color: rgb(237, 236, 236); padding: 4px 4px 4px 10px; background-image: url(../resource/images/table_bg.gif);" +
                 "font-family: Tahoma, Arial; font-size: 11px; font-style: normal; font-variant: normal;" +
                 "letter-spacing: normal; line-height: normal; orphans: auto; text-align: start;" +
                 "text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px;" +
                 "-webkit-text-stroke-width: 0px;'>" +
                 "<div class='spamRuleBroken_row_rulename' style='float: left; padding: 3px 0px 3px 5px;'>" +
                 "Quy tắc vi phạm</div>" +
                 "<div class='spamRuleBroken_row_rulescore' style='float: right; width: 80px; text-align: right;" +
                 "padding: 3px 15px 3px 5px;'>" +
                 "Điểm</div>" +
                "&nbsp;</div>";

                #endregion
                //Đánh giá nội dung
                foreach (DataRow RowItem in spam.Rows)
                {
                    key = " " + RowItem["Keyword"].ToString() + " ";
                    ItemScore = float.Parse(RowItem["Score"].ToString());
                    if (contentData.Contains(key) == true)
                    {
                        //Bắt đầu vòng lặp tính điểm tiêu đề
                        ruleContent += "<div class='spam_info spamRuleBroken_row' style='padding: 4px 8px; background-color: rgb(249, 249, 249);" +
                                     "display: block; clear: both; color: rgb(103, 103, 103); font-family: Tahoma, Arial;" +
                                     "font-size: 11px; font-style: normal; font-variant: normal; font-weight: normal;" +
                                     "letter-spacing: normal; line-height: normal; orphans: auto; text-align: start;" +
                                     "text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px;" +
                                     "-webkit-text-stroke-width: 0px;'>";
                        ruleContent += "<div class='spamRuleBroken_row_rulename' style='float: left; padding: 3px 0px 3px 5px;'>" + "Chứa từ vi phạm '" + key + "'" + "</div>" +
                         "<div class='spamRuleBroken_row_rulescore' style='float: right; width: 80px; text-align: right;" + "padding: 3px 15px 3px 5px;'>" + ItemScore + "</div></br>";
                        ruleContent += "</div>";
                        totalScoreContent = totalScoreContent + ItemScore;
                        // Kết thúc vòng lặp tính điểm tiêu đề
                    }
                }

                //Kết thúc đánh giá nội dung
                #region End
                ruleContent += "" +
                  "<div class='spam_info spamRuleBroken_row' style='padding: 4px 8px; background-color: rgb(249, 249, 249);" +
                  "display: block; clear: both; color: rgb(103, 103, 103); font-family: Tahoma, Arial;" +
                  "font-size: 11px; font-style: normal; font-variant: normal; font-weight: normal;" +
                  "letter-spacing: normal; line-height: normal; orphans: auto; text-align: start;" +
                  "text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px;" +
                  "-webkit-text-stroke-width: 0px;'>" +
                  "<div class='spamRuleBroken_graph' style='border: 1px solid gray; height: 5px; background-color: rgb(238, 238, 238);'>" +
                  "<div class='spam_notspam' style='background-color: rgb(0, 255, 0); height: 5px; width: 50%;'>" +
                  "<img height='5' src='../../resource/images/1x1.gif' width='1' /></div>" + "</div>" +
                   "<div style='line-height: 1; margin-bottom: 5px;'>" + "<br />" +
                   "Nội dung được đánh giá cao nhất<span class='Apple-converted-space'>&nbsp;</span><span" +
                   " style='font-size: 13px; font-weight: bold;'>" + totalScoreContent.ToString() + "</span><span class='Apple-converted-space'>&nbsp;</span>trong" +
                   " ngưỡng cho phép là 5. Điều này có nghĩa email của bạn sẽ được gửi đến đích, nhưng" +
                   "điều này không được bảo đảm tuyệt đối nó chỉ có giá trị tham khảo." +
                    "</div>" +
                "</div>";
                #endregion
                #endregion
                return ruleContent;
            }
            catch (Exception)
            {
                return null;
            }

        }
        [System.Web.Services.WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public static string contentUpdate(string content)
        {
            string con = content;
            return null;
        }
        protected void lbtEdit_Click1(object sender, EventArgs e)
        {
            LinkButton button = (LinkButton)sender;
            DataTable dtContent = (DataTable)Session["listContentSendEvent"];
            if (dtContent != null && dtContent.Rows.Count > 0)
            {
                DataRow[] rows = dtContent.Select("id=" + button.CommandArgument);
                if (rows.Length > 0)
                {
                    txtSubject.Text = rows[0]["subject"] + "";
                    txtBody.Text = rows[0]["body"] + "";
                    txtOrderNo.Text = rows[0]["OrderNo"] + "";
                    txtHour.Text = rows[0]["hoursend"] + "";
                    try
                    {
                        if (rows[0]["contentid"] + "" != "")
                            drlContent.Text = rows[0]["contentid"] + "";
                    }
                    catch { }
                    hdfContentIdEdit.Value = button.CommandArgument;
                    adhjskd.InnerHtml = "<span style='margin-right:10px;' class='fa fa-upload'></span>Cập nhật";
                    txtHour.Enabled = false;
                }
            }
        }
    }

}